package com.mrroman.linksender.gui;
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;
import javax.swing.text.JTextComponent;

public class CommonPopupMenu implements AWTEventListener {

	@Override
	public void eventDispatched(AWTEvent event) {
		if (event instanceof MouseEvent) {
			MouseEvent mouseEvent = (MouseEvent) event;

			if (mouseEvent.isPopupTrigger()) {

				Object source = event.getSource();
				if (source instanceof JTextComponent) {
					JTextComponent jTC = (JTextComponent) source;

                                        // check if text component is focusable

                                        if (!jTC.isFocusable())
                                            return;

                                        // if there are any mouse listeners registered
					// it means user added custom popup listener

//					for (MouseListener mouseListener : jTC.getMouseListeners()) 
// 						if (!mouseListener.getClass().getName().startsWith("java.") &&
// 								!mouseListener.getClass().getName().startsWith("javax."))
//							return;
					
					JPopupMenu menu = new JPopupMenu();
					menu.add(new CutAction(jTC));
					menu.add(new CopyAction(jTC));
					menu.add(new PasteAction(jTC));
					menu.add(new DeleteAction(jTC));
					menu.addSeparator();
					menu.add(new SelectAllAction(jTC));
					
					
					menu.show(jTC, mouseEvent.getX(), mouseEvent.getY());
				}
			}

		}
	}
	
	private class CutAction extends AbstractAction {

		private JTextComponent jtc;
		
		public CutAction(JTextComponent jtc) {
			super("Cut");
			this.jtc = jtc;
			setEnabled(jtc.getSelectionStart() != jtc.getSelectionEnd() &&
                                jtc.isEditable());
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jtc.cut();
		}
		
	}
	
	private class CopyAction extends AbstractAction {

		private JTextComponent jtc;
		
		public CopyAction(JTextComponent jtc) {
			super("Copy");
			this.jtc = jtc;
			setEnabled(jtc.getSelectionStart() != jtc.getSelectionEnd());
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jtc.copy();
		}
		
	}

	private class PasteAction extends AbstractAction {

		private JTextComponent jtc;
		
		public PasteAction(JTextComponent jtc) {
			super("Paste");
			this.jtc = jtc;
			setEnabled(
                                Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null) != null &&
                                jtc.isEditable());
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jtc.paste();
		}
		
	}
	
	private class DeleteAction extends AbstractAction {

		private JTextComponent jtc;
		
		public DeleteAction(JTextComponent jtc) {
			super("Delete");			
			this.jtc = jtc;
			setEnabled(jtc.getSelectionStart() != jtc.getSelectionEnd() &&
                                jtc.isEditable());
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jtc.setText(
					jtc.getText().substring(0, jtc.getSelectionStart()) + jtc.getText().substring(jtc.getSelectionEnd())
					);
		}
		
	}
	
	private class SelectAllAction extends AbstractAction {

		private JTextComponent jtc;
		
		public SelectAllAction(JTextComponent jtc) {
			super("Select all");
			this.jtc = jtc;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jtc.selectAll();
		}
		
	}

}
